package com.wk.warehouse.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wk.warehouse.entity.Auth;
import com.wk.warehouse.entity.Result;
import com.wk.warehouse.entity.User;
import com.wk.warehouse.service.AuthService;
import com.wk.warehouse.util.CurrentUser;
import com.wk.warehouse.util.TokenUtils;
import com.wk.warehouse.util.WarehouseConstants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/auth")
public class AuthController {

	@Autowired
	private AuthService authService;

	// 获取所有权限列表
	@GetMapping("/auth-tree")
	public Result allAuth(){
		// 所有一级权限
		List<Auth> authList = authService.firstAuthList();
		for(Auth firstAuth: authList){
			// 一级权限id
			int firstAuthId = firstAuth.getAuthId();
			// 所有二级权限
			List<Auth> secondAuthList = authService.selectAllChild(firstAuthId);
			// 设置一级权限的子权限
			firstAuth.setChildAuth(secondAuthList);
			for(Auth secondAuth: secondAuthList){
				int secondAuthId = secondAuth.getAuthId();
				// 获取三级权限
				List<Auth> thirdAuthList = authService.selectAllChild(secondAuthId);
				// 设置二级权限的子权限
				secondAuth.setChildAuth(thirdAuthList);
			}
		}
		return Result.ok(authList);
	}

	/**
	 * 验证name的唯一性
	 */
	@GetMapping("/name-check")
	public Result checkName(String authName) {
		// 表中authName的条数
		int num = authService.findAuthByName(authName);
		return Result.ok(num==0);
	}

	/**
	 * 验证url的唯一性
	 */
	@GetMapping("/url-check")
	public Result checkUrl(String authUrl) {
		// 表中authUrl的条数
		int num = authService.findAuthByUrl(authUrl);
		return Result.ok(num==0);
	}

	/**
	 * 验证code的唯一性
	 */
	@GetMapping("/code-check")
	public Result checkCode(String authCode) {
		// 表中authCode的条数
		int num = authService.findAuthByCode(authCode);
		return Result.ok(num==0);
	}

	/**
	 * 添加权限
	 */
	@PostMapping("/auth-add")
	public Result addAuth(@RequestBody Auth auth, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken) {
		// 验证权限name的唯一性
		if(authService.findAuthByName(auth.getAuthName())>0) return Result.err(Result.CODE_ERR_BUSINESS, "该权限name已存在！");
		// 验证权限url的唯一性
		if(authService.findAuthByName(auth.getAuthUrl())>0) return Result.err(Result.CODE_ERR_BUSINESS, "该权限url已存在！");
		// 验证权限code的唯一性
		if(authService.findAuthByName(auth.getAuthCode())>0) return Result.err(Result.CODE_ERR_BUSINESS, "该权限code已存在！");
		// 获取创建人id
		CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
		int createBy = currentUser.getUserId();
		// 放入auth对象中
		auth.setCreateBy(createBy);
		authService.insertAuth(auth);
		return Result.ok("添加权限成功！");
	}

	/**
	 * 删除权限
	 */
	@DeleteMapping("/auth-delete/{authId}")
	public Result deleteAuth(@PathVariable Integer authId) {
		authService.deleteAuth(authId);
		return Result.ok("删除权限成功！");
	}

	/**
	 * 启用权限
	 */
	@PutMapping("/auth-enable/{authId}")
	public Result enableAuth(@PathVariable Integer authId){
		authService.updateState(authId, 1);
		return Result.ok("启用权限成功！");
	}

	/**
	 * 禁用权限
	 */
	@PutMapping("/auth-disable/{authId}")
	public Result disableAuth(@PathVariable Integer authId){
		authService.updateState(authId, 0);
		return Result.ok("禁用权限成功！");
	}

	/**
	 * 修改权限
	 */
	@PutMapping("/auth-update")
	public Result updateAuth(@RequestBody Auth auth, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken) {
		// 获取修改人id
		CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
		int updateBy = currentUser.getUserId();
		auth.setUpdateBy(updateBy);

		int updateRows = authService.updateAuth(auth);
		if(updateRows>0){
			return Result.ok("修改权限成功！");
		}else{
			return Result.err(Result.CODE_ERR_SYS, "修改权限失败！");
		}
	}
}
